package com.example.unique.ui.gallery


import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.unique.R
import com.example.unique.databinding.ItemJobBinding
import com.example.unique.databinding.ItemMemberBinding
import com.example.unique.ui.chat.Member
import com.example.unique.ui.chat.MemberAdapter.MemberViewHolder

class JobAdapter(
    private val onViewDetailsClicked: (String) -> Unit
) : ListAdapter <Job, JobAdapter.JobViewHolder>(JobDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): JobViewHolder {
        val binding = ItemJobBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return JobViewHolder(binding)
    }
    override fun onBindViewHolder(holder: JobViewHolder, position: Int) {
        val job = getItem(position)
        holder.bind(job)
    }

    inner class JobViewHolder(private val binding: ItemJobBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(job: Job) {
            binding.taskName.text=job.taskName
            binding.startTime.text=job.startTime
            binding.endTime.text=job.endTime

            // 根据任务状态动态设置背景颜色和状态文本
            when (job.jobStatus) {
                1 -> {  // 进行中的任务
                    binding.jobStatus.text = "进行中"
                    binding.root.setBackgroundColor(
                        ContextCompat.getColor(binding.root.context, R.color.white)
                    )
                }
                2 -> {  // 任务完成
                    binding.jobStatus.text = "已完成"
                    binding.root.setBackgroundColor(
                        ContextCompat.getColor(binding.root.context, R.color.white)
                    )

                }
                3 -> {  // 已过期的任务
                    binding.jobStatus.text = "已过期"
                    binding.root.setBackgroundColor(
                        ContextCompat.getColor(binding.root.context, R.color.light_gray)
                    )
                    binding.startTime.setTextColor(
                        ContextCompat.getColor(binding.root.context, R.color.mauve_500)
                    )
                    binding.endTime.setTextColor(
                        ContextCompat.getColor(binding.root.context, R.color.mauve_500)
                    )
                }
                0 -> {  // 未开始的任务
                    binding.jobStatus.text = "未开始"
                    binding.root.setBackgroundColor(
                        ContextCompat.getColor(binding.root.context, R.color.light_gray_3)
                    )
                }
            }

            // 添加按钮点击事件
            binding.viewDetailsButton.setOnClickListener {
                // 将 taskName 的文本传递到 Fragment
              //  onViewDetailsClicked(job.taskName)
                try {
                    onViewDetailsClicked(job.taskName)
                } catch (e: Exception) {
                    Log.e("GalleryFragment", "导航到详情页时发生错误: ${e.message}", e)
                }

            }
        }
    }

    class JobDiffCallback : DiffUtil.ItemCallback<Job>() {
        override fun areItemsTheSame(oldItem: Job, newItem: Job): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Job, newItem: Job): Boolean {
            return oldItem == newItem
        }
    }


}





